package com.shujia.jdbc.pool;

import com.alibaba.druid.pool.DruidDataSource;
import org.junit.Test;

import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;

public class DruidPoolTest {

    @Test
    public void test1() throws Exception {
        DruidDataSource dataSource = new DruidDataSource();
        InputStream is = DruidPoolTest.class.getClassLoader()
                .getResourceAsStream("config\\druid.properties");
        Properties prop = new Properties();
        prop.load(is);

        dataSource.setUrl(prop.getProperty("jdbcUrl"));
        dataSource.setUsername(prop.getProperty("username"));
        dataSource.setPassword(prop.getProperty("password"));
        dataSource.setInitialSize(Integer.valueOf(prop.getProperty("initialSize")));
        dataSource.setMaxActive(Integer.valueOf(prop.getProperty("maxActive")));
        dataSource.setMinIdle(Integer.valueOf(prop.getProperty("minldle")));
        dataSource.setMaxWait(Integer.valueOf(prop.getProperty("maxWait")));


        for (int i = 1; i <= 11; i++) {
            Connection connection = dataSource.getConnection();
            System.out.println(connection);

            if (i == 3) {
                connection.close();
            }

        }
    }

    public static void main(String[] args) throws Exception {
        // 创建连接池对象
        DruidDataSource dataSource = new DruidDataSource();
        InputStream is = DruidPoolTest.class.getClassLoader()
                .getResourceAsStream("config\\druid.properties");
        Properties prop = new Properties();
        prop.load(is);

        dataSource.setUrl(prop.getProperty("jdbcUrl"));
        dataSource.setUsername(prop.getProperty("username"));
        dataSource.setPassword(prop.getProperty("password"));
        dataSource.setInitialSize(Integer.valueOf(prop.getProperty("initialSize")));
        dataSource.setMaxActive(Integer.valueOf(prop.getProperty("maxActive")));
        dataSource.setMinIdle(Integer.valueOf(prop.getProperty("minldle")));
        dataSource.setMaxWait(Integer.valueOf(prop.getProperty("maxWait")));

        // 返回连接
        Connection connection = dataSource.getConnection();
        System.out.println(connection);

    }
}
